﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;

public partial class Manage_Cms_ServerItem : System.Web.UI.Page
{
    protected KeRui.BLL.CmsServer bllCmsServer;
    protected KeRui.Model.CmsServer modelCmsServer = new KeRui.Model.CmsServer();
    public string Act = ManageHelper.PageAct();
    private ManageErrMsg nsb;
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();//登陆判断
        ManageHelper.CheckAdminPower("system_servermanage");//权限
        nsb = new ManageErrMsg();
        bllCmsServer = new KeRui.BLL.CmsServer();
        switch (Act)
        {
            case "addsave":
                Add();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            default:
                break;
        }

    }

    #region 添加
    private void Add()
    {
        ManageHelper.CheckAdminPower("system_serveradd");//权限
        bllCmsServer = new KeRui.BLL.CmsServer();
        modelCmsServer.ServerName = Utils.ChkSQL(PageRequest.GetString("ServerName"));
        if (modelCmsServer.ServerName == "")
        {
            nsb.AddErr("请填写服务器名称。");
        }
        modelCmsServer.LicenseID = PageRequest.GetInt("LicenseID", -1);
        if (modelCmsServer.LicenseID == -1)
        {
            nsb.AddErr("请选择许可证。");
        }
        modelCmsServer.ServerUrl = Utils.ChkSQL(PageRequest.GetString("ServerUrl"));
        if (modelCmsServer.ServerUrl == "")
        {
            nsb.AddErr("请填写服务器URL。");
        }
        modelCmsServer.ServerDir = Utils.ChkSQL(PageRequest.GetString("ServerDir"));
        if (modelCmsServer.ServerUrl == "")
        {
            nsb.AddErr("请填写服务系统路径。");
        }
        modelCmsServer.ServerKey = Utils.ChkSQL(PageRequest.GetString("ServerKey"));
        if (modelCmsServer.ServerKey == "")
        {
            nsb.AddErr("请填写通讯密钥。");
        }
        modelCmsServer.OrderID = PageRequest.GetInt("OrderID", -1);
        if (modelCmsServer.OrderID == -1)
        {
            nsb.AddErr("排序应该为一个整数。");
        }
        modelCmsServer.IsUsing = PageRequest.GetInt("IsUsing", 0);
        nsb.ChkErr();
        bllCmsServer.Add(modelCmsServer);
        ManageHelper.AddLog("system_serveradd", "添加了“" + modelCmsServer.ServerName + "”服务器。");
        Response.Write("<script type=\"text/javascript\">window.parent.location.href='serverlist.aspx?sucmsg=添加成功。'</script>");

    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {
        ActFlag = "editsave";
        ActName = "修改";
        int ServerID = PageRequest.GetInt("ID", -1);
        if (ServerID == -1)
        {
            nsb.AddErr("该站点不存在。");
            nsb.ChkErr();
        }
        modelCmsServer = bllCmsServer.GetModel(ServerID);
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("system_serveredit");//权限
        modelCmsServer.ServerID = PageRequest.GetInt("ServerID", -1);
        if (modelCmsServer.ServerID == -1)
        {
            nsb.AddErr("要修改的服务器不存在。");
            nsb.ChkErr();
        }
        modelCmsServer.ServerName = Utils.ChkSQL(PageRequest.GetString("ServerName"));
        if (modelCmsServer.ServerName == "")
        {
            nsb.AddErr("请填写服务器名称。");
        }
        modelCmsServer.LicenseID = PageRequest.GetInt("LicenseID", -1);
        if (modelCmsServer.LicenseID == -1)
        {
            nsb.AddErr("请选择许可证。");
        }
        modelCmsServer.ServerUrl = Utils.ChkSQL(PageRequest.GetString("ServerUrl"));
        if (modelCmsServer.ServerUrl == "")
        {
            nsb.AddErr("请填写服务器URL。");
        }
        modelCmsServer.ServerDir = Utils.ChkSQL(PageRequest.GetString("ServerDir"));
        if (modelCmsServer.ServerUrl == "")
        {
            nsb.AddErr("请填写服务系统路径。");
        }
        modelCmsServer.ServerKey = Utils.ChkSQL(PageRequest.GetString("ServerKey"));
        if (modelCmsServer.ServerKey == "")
        {
            nsb.AddErr("请填写通讯密钥。");
        }
        modelCmsServer.OrderID = PageRequest.GetInt("OrderID", -1);
        if (modelCmsServer.OrderID == -1)
        {
            nsb.AddErr("排序应该为一个整数。");
        }
        modelCmsServer.IsUsing = PageRequest.GetInt("IsUsing", 0);
        nsb.ChkErr();
        bllCmsServer.Update(modelCmsServer);
        ManageHelper.AddLog("system_serveredit", "修改了“" + modelCmsServer.ServerName + "”服务器。");
        Response.Write("<script type=\"text/javascript\">window.parent.location.href='serverlist.aspx?act=edit&id=" + modelCmsServer.ServerID + "&sucmsg=修改成功。'</script>");
    }
    #endregion

    #region 删除
    private void Del()
    {
        ManageHelper.CheckAdminPower("system_serverdel");//权限
        int ServerID = PageRequest.GetInt("ID", -1);
        if (ServerID == -1)
        {
            nsb.AddErr("该服务器不存在。");
            nsb.ChkErr();
        }
        int retInt = bllCmsServer.Delete(ServerID);
        if (retInt == 1)
        {
            nsb.AddErr("该服务器下存在站点不能删除。");
            nsb.ChkErr();
        }
        else
        {
            ManageHelper.AddLog("system_serverdel", "删除了“" + modelCmsServer.ServerName + "”服务器。");
            Response.Redirect("ServerList.aspx?sucmsg=恭喜。删除成功。");
        }
    }
    #endregion
}
